package io.coderyeah.ymcc.mission;

import cn.hutool.core.date.DateUtil;
import io.coderyeah.ymcc.domain.KillActivity;
import io.coderyeah.ymcc.service.IKillActivityService;
import io.coderyeah.ymcc.util.AssertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author lqs
 * @date 2022/11/9 10:00
 */
@Component
@Slf4j
public class PublishKillCourse2Redis {
    @Autowired
    private IKillActivityService activityService;


    // 定时发布秒杀活动到redis  （0 0 1 * * ?）==>>每天凌晨一点执行一次
    // 每天凌晨0点执行一次  @Scheduled(cron = "0 0 0 * * ?")
    // @Scheduled(cron = "0 0/1 8-20 * * ?") // 每隔一分钟执行一次
    @Scheduled(cron = "0 0 0 * * ?")// 每天凌晨0点执行一次
    public void publish() {
        // 查询当天的数据sql:SELECT * FROM t_kill_activity WHERE TO_DAYS(start_time) =TO_DAYS(NOW())
        // 获取到当天待发布的活动
        List<KillActivity> activities = activityService.getCurrentData();
        AssertUtil.isNotNull(activities, "今日暂无秒杀活动发布");
        // 遍历每一个活动执行发布
        log.info("{}--任务准备发布到redis~~~", DateUtil.now());
        activities.forEach(killActivity -> {
            // 任务发布
            activityService.publish(killActivity.getId());
        });
        log.info("{}--已发布到redis~~~", DateUtil.now());
    }
}
